package com.yilin.platform.service.impl.order;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yilin.platform.base_core.application.PageData;
import com.yilin.platform.base_core.tools.QueryUtil;
import com.yilin.platform.entity.enums.user.OrganType;
import com.yilin.platform.entity.order.LineOrder;
import com.yilin.platform.entity.user.Account;
import com.yilin.platform.mapper.order.LineOrderMapper;
import com.yilin.platform.service.order.ILineOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Transactional(rollbackFor = Exception.class)
public class LineOrderService extends ServiceImpl<LineOrderMapper, LineOrder> implements ILineOrderService {
    @Resource
    private LineOrderMapper lineOrderMapper;

    @Override
    public void saveOrUpdateTineOrder(Account account, LineOrder lineOrder) {
        if(account.getOrganType()== OrganType.company){
            lineOrder.setCompany(true);
            lineOrder.setSuperiorPlatId(account.getOrganizeId());
            lineOrder.setSuperiorPlatName(account.getOrganizeName());
        }
        lineOrder.setAccountId(account.getId());
        lineOrder.setAccountName(account.getName());
        saveOrUpdate(lineOrder);
    }

    //获取分页
    @Override
    public PageData<LineOrder> getLineOrderPage(Account loginUser, LineOrder query, Integer start, Integer limit) {
        QueryWrapper<LineOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(wrapper ->
                wrapper.eq("account_id", loginUser.getId())
                        .or()
                        .eq("superior_plat_id", loginUser.getSuperiorPlatId())
                        .or()
                        .eq("superior_plat_id", loginUser.getOrganizeId())
        );
        QueryUtil.andQueryWrapper(queryWrapper, query);
        return lineOrderMapper.selectPage(new PageData<>(start, limit), queryWrapper);
    }

    //根据ID获取
    @Override
    public LineOrder getLineOrderById(String id) {
        return lineOrderMapper.selectById(id);
    }
    @Override
    public LineOrder getLineOrderByName(String name) {
        QueryWrapper<LineOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("send_name",name);
        return lineOrderMapper.selectOne(queryWrapper);
    }
    //删除
    @Override
    public void deleteLineOrder(String id) {
        lineOrderMapper.deleteById(id);
    }
}
